﻿

Public Class Compression
    'Shared :
    ''' <summary>
    ''' Default Compression parameters.
    ''' </summary>
    Shared Property Defaults As Compression
    Shared Sub New()
        Defaults = New Compression
    End Sub
    'Ctor : 
    ''' <summary>
    ''' Creates a new Compression instance.
    ''' </summary>
    Public Sub New()
        Me.Reset()
    End Sub
    'Fields :
    ''' <summary>
    ''' Compression Method.
    ''' </summary>
    Public Property Method As Zip.CompressionMethod
    ''' <summary>
    ''' Compression Level.
    ''' </summary>
    Public Property Level As Zlib.CompressionLevel
    ''' <summary>
    ''' Compression Strategy.
    ''' </summary>
    Public Property Strategy As Zlib.CompressionStrategy
    ''' <summary>
    ''' Alternative Text Encoding.
    ''' </summary>
    Public Property Encoding As System.Text.Encoding
    ''' <summary>
    ''' Alternative Text Encoding Usage.
    ''' </summary>
    Public Property EncodingUsage As Zip.ZipOption
    ''' <summary>
    ''' Zip64 Specification Usage.
    ''' </summary>
    Public Property Zip64Usage As Zip.Zip64Option

    'Routines :
    ''' <summary>
    ''' Resets Compression to default.
    ''' </summary>
    Public Sub Reset()
        Me.Method = Zip.CompressionMethod.Deflate
        Me.Level = Zlib.CompressionLevel.Default
        Me.Strategy = Zlib.CompressionStrategy.Default
        Me.Encoding = Zip.ZipFile.DefaultEncoding
        Me.EncodingUsage = Zip.ZipOption.Default
        Me.Zip64Usage = Zip.Zip64Option.Default
    End Sub

    ''' <summary>
    ''' Reset a given property in the Compression.
    ''' </summary>
    ''' <param name="propertyname">Property name to reset</param>
    Public Sub Reset(ByVal propertyname As String)
        Select Case propertyname
            Case "Method"
                Me.Method = Zip.CompressionMethod.Deflate
            Case "Level"
                Me.Level = Zlib.CompressionLevel.Default
            Case "Strategy"
                Me.Strategy = Zlib.CompressionStrategy.Default
            Case "Encoding"
                Me.Encoding = Zip.ZipFile.DefaultEncoding
            Case "EncodingUsage"
                Me.EncodingUsage = Zip.ZipOption.Default
            Case "Zip64Usage"
                Me.Zip64Usage = Zip.Zip64Option.Default
        End Select
    End Sub

    ''' <summary>
    ''' Copies values from another Compression object.
    ''' </summary>
    ''' <param name="comp">Compression from wich copy values</param>
    Public Sub CopyFrom(ByVal comp As Compression)
        Me.Method = comp.Method
        Me.Level = comp.Level
        Me.Strategy = comp.Strategy
        Me.Encoding = comp.Encoding
        Me.EncodingUsage = comp.EncodingUsage
        Me.Zip64Usage = comp.Zip64Usage
    End Sub

    ''' <summary>
    ''' Applies Compression parameters to the given ZIpFile object.
    ''' </summary>
    ''' <param name="zip">ZipFile instance</param>
    Public Sub ApplyToZip(ByVal zip As Zip.ZipFile)
        zip.CompressionMethod = Me.Method
        zip.CompressionLevel = Me.Level
        zip.Strategy = Me.Strategy
        zip.AlternateEncoding = Me.Encoding
        zip.AlternateEncodingUsage = Me.EncodingUsage
        zip.UseZip64WhenSaving = Me.Zip64Usage
    End Sub

    ''' <summary>
    ''' String informations representing Compression parameters.
    ''' </summary>
    Public Function Infos() As String
        Return String.Format("Compression : {0} - {1} - {2} - {3} - {4} - {5}", _
                             Me.Method.InfosString, Me.Level.InfosString, Me.Strategy.InfosString, _
                             Me.Encoding.InfosString, Me.EncodingUsage.InfosString, Me.Zip64Usage.InfosString)
    End Function
End Class
